package php

import (
	"hash/crc32"
	"math/rand"
	"strings"
	"time"
)

func Mb_subStr(str string, start int, length int) interface{} {
	if length == 0 || start < 0 {
		return false
	}
	rs := []rune(str)
	var strLen = len(rs)
	//fmt.Println(strLen)
	var end int
	if length > 0 {
		end = start + length
	} else {
		end = strLen + length
	}
	var sub []rune
	if end >= strLen {
		sub = rs[start:]
	} else {
		sub = rs[start:end]
	}
	return string(sub)
}

func Strlen(str string) int {
	return len(str)
}

func AddSlashes(str string, needle string) string {
	return strings.Replace(str, needle, "\\"+needle, -1)
}

func Crc32(str string) int {
	res := crc32.ChecksumIEEE([]byte(str))
	return int(res)
}

func Implode(strs []string, delimeter string) string {
	var res string
	var length = len(strs)
	for i, item := range strs {
		if i == length-1 {
			res += item
		} else {
			res += item + delimeter
		}
	}
	return res
}

func Ucfirst(str string) string {
	var slice = []rune(str)
	var res string
	for i, item := range slice {
		if i == 0 {
			if item < 97 || item > 122 {
				return str
			}
			//singleStr = strings.ToUpper(string(item))
			item -= 32
		}
		res += string(item)
	}
	return res
}

func Lcfirst(str string) string {
	var slice = []rune(str)
	var res string
	for i, item := range slice {
		if i == 0 {
			if item < 65 || item > 90 {
				return str
			}
			//singleStr = strings.ToUpper(string(item))
			item += 32
		}
		res += string(item)
	}
	return res
}

func Parsestr(str string) map[string]string {
	var slice = strings.Split(str, "&")
	var res = make(map[string]string)
	for _, item := range slice {
		equalSlice := strings.Split(item, "=")
		res[equalSlice[0]] = equalSlice[1]
	}
	return res
}

func Str_replace(search string, replace string, str string, count int) string {
	return strings.Replace(str, search, replace, count)
}

func Str_pad(str string, length int, padString string, padType string) string {
	var padStrings, res string
	padStrings = Str_repeat(padString, length)
	if padType == "left" {
		res += padStrings + str
	} else if padType == "right" {
		res += str + padStrings
	}
	return res
}

func Str_repeat(str string, repeat int) string {
	var res string
	for index := 0; index < repeat; index++ {
		res += str
	}
	return res
}

func Str_split(str string, length int) []string {
	var res []string
	strSlice := []rune(str)
	res1 := strSlice[0:length]
	res2 := strSlice[length:]
	res = append(res, string(res1), string(res2))
	return res
}

func Str_shuffle(str string) string {
	sliceStr := []rune(str)
	var randNum int
	rand.Seed(time.Now().UnixNano())
	for key, _ := range sliceStr {
		randNum = rand.Intn(len(sliceStr) - 1)
		if randNum != key {
			sliceStr[key], sliceStr[randNum] = sliceStr[randNum], sliceStr[key]
		}
	}
	return string(sliceStr)
}

func Str_pos(str string, sub string) int {
	return strings.Index(str, sub)
}

func Strstr(str string, sub string) string {
	var pos = strings.Index(str, sub)
	return str[pos:]
}

func Str_word_count(str string) int {
	//a 97 z 122 A 65 Z 90
	var lastItem rune
	var wordCount int
	for _, item := range str {
		if isLetter(item) && !isLetter(lastItem) {
			wordCount++
		}
		lastItem = item
	}
	return wordCount
}

func isLetter(letter rune) bool {
	if (letter >= 65 && letter <= 90) || (letter >= 97 && letter <= 122) {
		return true
	}
	return false
}
